package com.supermap.gwfs.datashare.of.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.print.attribute.standard.PDLOverrideSupported;

import com.supermap.gwfs.datashare.entity.GridData;
import com.supermap.gwfs.datashare.entity.StationInfo;
import com.supermap.gwfs.datashare.util.DataFormatUtil;
import com.supermap.gwfs.datashare.util.Logger;
import com.supermap.gwfs.datashare.util.MathArith;
import org.springframework.stereotype.Service;

/**  
 * @Description: 获取站点数据(最近格点代替)
 * @author zhoujian
 * @date 2017-8-24
 * @version V1.0 
 */
@Service
public class StationDataService
{
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	public Map<String, Map<String, List<Float>>> stationDataByGrid(List<StationInfo> stationInfos , List<GridData> gridDatas , Map<String, Integer> eleIntervalMap) 
	{
		//  站ID+时效              要素            值        
		Map<String, Map<String, List<Float>>> dataAndEleValMap = null;
		try
		{
			dataAndEleValMap = new HashMap<String, Map<String, List<Float>>>(); 
			//获取站点在文件中的X轴和Y轴坐标
			Map<String,int[]> stationAndLonLat = this.getStationIndex(stationInfos , gridDatas);
			
			for (GridData gd : gridDatas)//格点数据类
			{
				for (String stationId : stationAndLonLat.keySet())
				{
					//XY[0] 为列标
					//XY[1] 为行标
					int[] XY = stationAndLonLat.get(stationId);
					int row = XY[1];
					int clo = XY[0];
					if (clo >= gd.getCols() || row >= gd.getRows())
					{
						continue;
					}
					
					float value = DataFormatUtil.formatFloat(gd.getValues()[row][clo], 1);
					int interval = eleIntervalMap.get(gd.getElement());
					if (dataAndEleValMap.containsKey(stationId))
					{
						if (dataAndEleValMap.get(stationId).containsKey(gd.getElement()))
						{
							List<Float> tmpList = dataAndEleValMap.get(stationId).get(gd.getElement());
							//为了解决欧洲模式在60时效之后会是逐6小时的间隔带来的异常
							try
							{
								tmpList.get(gd.getValid() / interval - 2);
							}
							catch (Exception e)
							{
								dataAndEleValMap.get(stationId).get(gd.getElement()).add(gd.getValid() / interval - 2 , 999999.9f);
							}
							dataAndEleValMap.get(stationId).get(gd.getElement()).add(gd.getValid() / interval - 1, value);
						}
						else {
							List<Float> list = new ArrayList<Float>();
							if (gd.getValid() / interval - 2 >= 0)
							{
								list.add(gd.getValid() / interval - 2 , 999999.9f);
							}
							list.add(gd.getValid() / interval - 1 , value);
							dataAndEleValMap.get(stationId).put(gd.getElement(), list);
						}
					}
					else {
						Map<String, List<Float>> tmpMap = new HashMap<String, List<Float>>();
						List<Float> list = new ArrayList<Float>();
						int valid = gd.getValid();
						if (valid / interval - 2 >= 0)
						{
							list.add(valid / interval - 2 , 999999.9f);
						}
						list.add(valid / interval - 1, value);
						tmpMap.put(gd.getElement(), list);
						dataAndEleValMap.put(stationId, tmpMap);
					}
					
				}
			}
		}
		catch (Exception e)
		{
			logger.error("Grid to station data exception , this exception is : " + e);
		}
		return dataAndEleValMap;
	}
	/**
	 * 
	 * @Description: 站点经纬度转X Y轴
	 * @return Map<String,int[]>
	 * @throws
	 */
	private Map<String, int[]> getStationIndex(List<StationInfo> stationInfos,
			List<GridData> gridDatas)
	{
		Map<String, int[]> stationAndLonLat = new HashMap<String, int[]>();
		try
		{
			if (gridDatas.size() > 0)
			{
				GridData gd = gridDatas.get(0);
				for (StationInfo s : stationInfos)
				{
					String staionId = s.getStation_id();
					int[] XY = getXyByLonLat(s.getLon(), s.getLat(), gd.getStartLon(), gd.getStartLat(), gd.getLonResolution(), gd.getLatResolution());
					stationAndLonLat.put(staionId, XY);
				}
			}
		}
		catch (Exception e)
		{
			logger.error("Station latitude and longitude to XY exception , this exception is : " + e);
		}
		return stationAndLonLat;
	}

	//获取站点在文件中的X Y轴坐标
	public int[] getXyByLonLat(float lon , float lat , float startLon , float startLat , float lonResolution , float latResolution)
	{
		int[] XY = new int[2];
		int X = (int)MathArith.div(lon - startLon , lonResolution, 0);
		int Y = (int)MathArith.div(lat - startLat , latResolution, 0);
		//[0]为行标
		XY[0] = X;
		//[1]为列标
		XY[1] = Y;
		
		return XY;
	}
	
}
